package com.tarena.lbs.coupon.controller;

import com.tarena.lbs.activity.api.ActivityApi;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import com.tarena.lbs.coupon.service.StockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处理和优惠券数据业务有关的接口
 */
@RestController
@Slf4j
@Tag(name="优惠券模块")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private StockService stockService;

    @DubboReference
    private ActivityApi activityApi;


    @Operation(summary = "查询当前商家优惠券分页列表")
    @PostMapping("/admin/marketing/coupon/info/list")
    public Result<PageResult<CouponVO>> pageList(@RequestBody BasePageQuery pageQuery) throws BusinessException{
        log.debug("查询当前商家优惠券分页列表: pageQuery={}",pageQuery);
        PageResult<CouponVO> pageResult = couponService.pageList(pageQuery);
        return new Result<>(pageResult);
    }


    @Operation(summary = "保存优惠券")
    @PostMapping("/admin/marketing/coupon/info/save")
    public Result<Void> saveCoupon(@RequestBody CouponParam couponParam)throws BusinessException{
        log.debug("保存优惠券：couponParam={}",couponParam);
        couponService.saveCoupon(couponParam);
        return Result.success();
    }

    @Operation(summary = "查询优惠券详情")
    @GetMapping("/admin/marketing/coupon/info/detail/{id}")
    public Result<CouponVO> detail(@PathVariable("id") Integer id)
        throws BusinessException{
        log.debug("查询优惠券详情：couponId={}",id);
        CouponVO couponVO = couponService.detail(id);
        return new Result<>(couponVO);
    }

    @Operation(summary = "删除优惠券")
    @PostMapping("/admin/marketing/coupon/info/delete/{id}")
    public Result<Void> delete(@PathVariable Integer id)
            throws BusinessException{
        log.debug("删除优惠券：couponId={}",id);
        couponService.delete(id);
        return Result.success();
    }

    @Operation(summary = "领取优惠券")
    @PostMapping("/marketing/user/receive/save")
    public Result<Void> receiveCoupon(@RequestBody UserCouponsParam param)
        throws BusinessException{
        log.debug("领取优惠券：UserCouponsParam={}",param);
        stockService.receiveCoupon(param);
        return Result.success();
    }


    @Operation(summary = "用户领取优惠券列表")
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> receiveList(@RequestBody UserCouponQuery couponQuery)throws BusinessException{
        log.debug("用户领取优惠券列表：couponQuery={}",couponQuery);
        PageResult<UserCouponsVO> pageResult = couponService.receiveList(couponQuery);
        return new Result<>(pageResult);
    }


    @Operation(summary = "查看优惠券码的二维码图片")
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> receiveDetail(@RequestBody UserCouponCodeQuery query)
        throws BusinessException{
        log.debug("查看优惠券码的二维码图片：userCouponCodeQuery={}",query);
        UserCouponsVO userCouponsVO = couponService.receiveDetail(query);
        return new Result<>(userCouponsVO);
    }

    @Operation(summary = "优惠券核销")
    @PostMapping("/marketing/user/receive/use/{userId}/{couponCode}/{orderNo}")
    public Result<Void> useReceive(@PathVariable Integer userId, @PathVariable String couponCode,@PathVariable String orderNo)
        throws BusinessException{
        log.debug("优惠券核销：userId={}，couponCode={}, orderNo={}",userId,couponCode,orderNo);
        couponService.useReceive(userId,couponCode,orderNo);
        return Result.success();
    }


}
